package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.entity.IdtWarnRule;
import com.guodi.model.service.IIdtWarnRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * @描述：指标预警规则表控制类
 * @作者: 戴伙进
 * @日期: 2020-06-11
 */
@RestController
@RequestMapping("/IdtWarnRule")
@Api(value = "API - IdtWarnRuleController", description = "指标预警规则表")
public class IdtWarnRuleController extends BaseController {

    @Resource
    public IIdtWarnRuleService idtWarnRuleService;

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 戴伙进
    * @日期: 2020-06-11
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    public AjaxResult listByEntity(Page page ){
        LambdaQueryWrapper<IdtWarnRule> wrapper = new QueryWrapper().lambda();
        IPage<IdtWarnRule> iPage = idtWarnRuleService.page(page, wrapper);
        return new AjaxResult(iPage);
    }

    /**
     * @描述: 查询所有预警规则
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020-06-11
     **/
    @PostMapping("/listAll")
    @ApiOperation(value = " 查询所有预警规则")
    public AjaxResult listAll(){
        List<IdtWarnRule> list = idtWarnRuleService.list();
        return new AjaxResult(list);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020-06-11
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id) {
        if(StringUtils.isBlank(id)){
            return new AjaxResult(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        IdtWarnRule idtWarnRule = idtWarnRuleService.getById(id);
        return new AjaxResult(idtWarnRule);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020-06-11
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public AjaxResult save(IdtWarnRule idtWarnRule){
        prepareData(idtWarnRule, true);
        //设置主键
        idtWarnRule.setId(UuidUtil.get32UUID());
        idtWarnRuleService.save(idtWarnRule);
        return new AjaxResult(idtWarnRule);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020-06-11
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(IdtWarnRule idtWarnRule) {
        prepareData(idtWarnRule, false);
        idtWarnRuleService.updateById(idtWarnRule);
        return new AjaxResult(idtWarnRule);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020-06-11
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        idtWarnRuleService.removeById(id);
        return new AjaxResult();
    }

    /**
     * @描述:删除多条记录
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020/6/15 16:55
     **/
    @PostMapping("/deleteByIds")
    @ApiOperation(value = "根据多个id删除,多个id用,隔开")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult deleteByIds(String ids){
        String[] idsAry = ids.split(",");
        List<String> idList = Arrays.asList(idsAry);
        idtWarnRuleService.removeByIds(idList);
        return new AjaxResult();
    }

    /**
     * @描述:列出查询的字典项
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020/6/11 11:44
     **/
    @PostMapping("/listDictionary")
    @ApiOperation(value = "列出字典项,id跟名称")
    public AjaxResult listDictionary(){
        List<Map<String,Object>> retList = new ArrayList<>();
        LambdaQueryWrapper<IdtWarnRule> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.orderByAsc(IdtWarnRule::getId);
        List<IdtWarnRule> list = idtWarnRuleService.list(queryWrapper);
        for (int i = 0; i <list.size() ; i++) {
            IdtWarnRule idtWarnRule =list.get(i);
            HashMap map = new HashMap();
            map.put("id",idtWarnRule.getId());
            map.put("name",idtWarnRule.getName());
            retList.add(map);
        }
        return new AjaxResult(retList);
    }

    /**
     * @描述:表单预设值
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020/6/15 16:19
    **/
    private void prepareData(IdtWarnRule idtWarnRule, boolean isSave) {
        if(isSave){
            idtWarnRule.setCreator(this.getUser().getUserId().toString());
            idtWarnRule.setCreateTime(new Date());
            //防止误修改
            idtWarnRule.setModifier(null);
            idtWarnRule.setModifyTime(null);
        }else{
            idtWarnRule.setModifier(this.getUser().getUserId().toString());
            idtWarnRule.setModifyTime(new Date());
            //防止误修改
            idtWarnRule.setCreator(null);
            idtWarnRule.setCreateTime(null);
        }
    }
}
